const Redis = require("ioredis");
const Config = require("../../config");
const RedisConfig = Config.Radis;
let { host, port, username, password,project } = RedisConfig;
// 创建一个客户端连接到Redis服务器
const RedisClient = new Redis({
  port,
  host,
  username,
  password,
  db: 0, // Defaults to 0
  reconnectOnError(error) {
    const targetError = "READONLY";
    if (error.message.includes(targetError)) {
      return true;
    }
  },
});

RedisClient.ProjectName = project

// 监听错误事件
RedisClient.on("error", (err) => {
  let { code, message } = err;
  console.log("Redis Client Error Message:", message);
  console.log("Redis Client Error Code:", code);
  console.log("Redis Client Error", err);
  if (code == "ENETUNREACH") {
    console.log("Redis 服务已断开");
    // Redis 重新连接
    RedisClient.connect();
  } else {
    console.log(code);
  }
});
module.exports = RedisClient;
